<template>
  <div class="fdsBusinessStartRuleset-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="编码" prop="number">
          <el-input
            placeholder="请输入营业准备规则编码"
            v-model="searchForm.number"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            placeholder="请输入营业准备规则名称"
            v-model="searchForm.name"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="btns-fun-field">
        <!-- v-if="userPermission.addUserPerm" -->
        <el-button
          type="primary"
          @click.prevent="showAddEditDialog"
          :icon="Plus"
        >
          新增营业准备规则
        </el-button>
      </div>
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="number" label="编码" width="120" />
          <el-table-column prop="name" label="规则名称" width="160" />
          <!-- <el-table-column prop="dayTime" label="日期" width="190" /> -->
          <!-- 操作物料数量 -->
          <el-table-column
            prop="operationMaterial"
            label="关联物料"
            width="150"
          >
            <template #default="scope">
              <div class="table-column-item">
                <span>{{ scope.row.operationMaterial || 0 }}</span>
                <el-button
                  size="small"
                  style="margin-left: 12px"
                  type="primary"
                  @click="showRelationalDialog(scope.row)"
                  >设置物料</el-button
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="defaultValue" label="是否默认规则" width="110">
            <template #default="scope">
              <div class="table-column-item">
                <span v-if="scope.row.defaultValue == 1">是</span>
                <span v-else>否</span>
              </div>
            </template>
          </el-table-column>
          <!-- 操作机器数量 -->
          <el-table-column prop="operationMachine" label="应用机台" width="150">
            <template #default="scope">
              <div class="table-column-item">
                <span>{{ scope.row.operationMachine || 0 }}</span>
                <el-button
                  size="small"
                  style="margin-left: 12px"
                  type="primary"
                  @click="showRelationalDeviceDialog(scope.row)"
                  >绑定机台</el-button
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="enabled" label="状态" width="80">
            <template #default="scope">
              <div class="table-column-item">
                <el-tag type="success" v-if="scope.row.enabled">启用</el-tag>
                <el-tag type="danger" v-else>禁用</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="190" />
          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->
              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <el-button
                v-if="scope.row.enabled"
                type="danger"
                size="small"
                @click.prevent="showEnabledDialog(scope.row, 2)"
                >禁用</el-button
              >
              <el-button
                v-else
                type="success"
                size="small"
                @click.prevent="showEnabledDialog(scope.row, 2)"
                >启用</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <FdsBusinessStartRulesetAdd
      v-model="addUpdateRef"
    ></FdsBusinessStartRulesetAdd>
    <FdsBusinessStartRulesetRemove
      v-model="removeRef"
    ></FdsBusinessStartRulesetRemove>
    <FdsBusinessStartRulesetEnable v-model="enabledRef">
    </FdsBusinessStartRulesetEnable>

    <RelationalMaterial v-model="relationalDialogRef" />
    <RelationalDeviceList v-model="relationalDeviceDialogRef" />
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getFdsBusinessStartRulesetPagination,
  getFdsBusinessStartRulesetAllList,
} from '@/api/fds-business-start-ruleset.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import FdsBusinessStartRulesetRemove from './FdsBusinessStartRulesetRemove.vue'
import FdsBusinessStartRulesetAdd from './FdsBusinessStartRulesetAdd.vue'
import FdsBusinessStartRulesetEnable from './FdsBusinessStartRulesetEnable.vue'
import RelationalMaterial from './RelationalMaterial.vue'
import RelationalDeviceList from './RelationalDeviceList.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'

const titleName = `营业准备规则`
// const userPermission = useUserPermission();

const enabledRef = reactive({
  visible: false,
  form: {
    id: '',
    enabled: '',
  },
  refreshDataList: () => {
    getDataList()
  },
})
const showEnabledDialog = (item: any) => {
  enabledRef.visible = true
  enabledRef.form.id = item.id
  enabledRef.form.enabled = item.enabled ? '0' : '1'
}

const relationalDeviceDialogRef = reactive({
  visible: false,
  form: {
    id: '',
    enabled: '',
  },
  refreshDataList: () => {
    getDataList()
  },
})

const showRelationalDeviceDialog = (item: any) => {
  relationalDeviceDialogRef.visible = true
  relationalDeviceDialogRef.form.id = item.id
}

// ======================================================

const relationalDialogRef = reactive({
  visible: false,
  form: {
    id: '',
    enabled: '',
  },
  refreshDataList: () => {
    getDataList()
  },
})

const showRelationalDialog = (item: any) => {
  relationalDialogRef.visible = true
  relationalDialogRef.form.id = item.id
}

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `营业准备规则`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

// const requestCanUseCode = () => {
// 	getCanUseCode({ type: 8 })
// 			.then((res: any) => {
// 				console.log('getCanUseCode: ', res)
// 				addUpdateRef.form.number = res
// 			})
// 			.catch((err) => {
// 				ElMessage.error(err.msg)
// 			})
// }

const showAddEditDialog = (item: any, type: number) => {
  //新增
  if (item.target) {
    let form = {
      id: null,
      number: '',
      name: '',
      enabled: '',
      dayTime: '',
      operationMaterial: '',
      operationMachine: '',
      defaultValue: false,
      remark: '',
      disabled: false,
    }
    addUpdateRef.rules = []
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    // requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    number: item.number,
    name: item.name,
    enabled: item.enabled,
    dayTime: item.dayTime,
    operationMaterial: item.operationMaterial,
    operationMachine: item.operationMachine,
    defaultValue: item.defaultValue == 1 ? true : false,
    remark: item.remark,
    disabled: false,
  }
  addUpdateRef.rules = []
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: `营业准备规则`,
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  number: '',
  name: '',
  enabled: '',
  dayTime: '',
  operationMaterial: '',
  operationMachine: '',
  defaultValue: '',
  remark: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 清空搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let number = searchForm.value.number
  if (number) {
    searchCondition.number = number
  }
  let name = searchForm.value.name
  if (name) {
    searchCondition.name = name
  }
  let enabled = searchForm.value.enabled
  if (enabled) {
    searchCondition.enabled = enabled
  }
  let dayTime = searchForm.value.dayTime
  if (dayTime) {
    searchCondition.dayTime = dayTime
  }
  let operationMaterial = searchForm.value.operationMaterial
  if (operationMaterial) {
    searchCondition.operationMaterial = operationMaterial
  }
  let operationMachine = searchForm.value.operationMachine
  if (operationMachine) {
    searchCondition.operationMachine = operationMachine
  }
  let defaultValue = searchForm.value.defaultValue
  if (defaultValue) {
    searchCondition.defaultValue = defaultValue
  }
  let remark = searchForm.value.remark
  if (remark) {
    searchCondition.remark = remark
  }

  tableLoading.value = true
  getFdsBusinessStartRulesetPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})
</script>
<style lang="scss" scoped>
.fdsBusinessStartRuleset-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.fdsBusinessStartRuleset-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}

.table-column-item {
  display: flex;
  align-items: center;
}
</style>
